package com.zzyl.common.utils;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class PDFUtil {

    public static String pdfToString(InputStream is){
        PDDocument pdDocument = null;
        try{
            //1
            pdDocument = PDDocument.load(is);

            //2
            PDFTextStripper pdfTextStripper = new PDFTextStripper();

            //3
            String text = pdfTextStripper.getText(pdDocument);
            return text;
        }catch(IOException ioe){
            ioe.printStackTrace();
        }finally {
            try{
                pdDocument.close();
                is.close();
            }catch (IOException ioe){
                ioe.printStackTrace();
            }
        }
        return null;
    }

    public static void main(String[] args) throws FileNotFoundException {
        FileInputStream fileInputStream = new FileInputStream("D:\\Study\\中州新养老\\中州养老课程资料\\09. 智能评估-集成AI大模型\\资料\\体检报告样例\\体检报告-刘爱国-男-69岁.pdf");

        String result = PDFUtil.pdfToString(fileInputStream);
        System.out.println(result);
    }

}
